import socket
import urllib
from bs4 import BeautifulSoup
import requests
import time


# 定义url
# city = input("请输入城市：")
url = "https://search.jd.com/Search?keyword=%E5%8F%A3%E7%BA%A2&enc=utf-8"


req = urllib.request.Request(url)
try:
    response = urllib.request.urlopen(req, timeout=10)
    print(response.info())
    respHtml = response.read()
    decodeHtml = respHtml.decode('utf-8')
    soup = BeautifulSoup(decodeHtml, 'html.parser')
    result_li = soup.find_all('li', class_='gl-item')
    for li in result_li:
        data_sku = li.get('data-sku')
        if data_sku is not None:
            # 请求页面的详情
            www_item_href = "https://item.jd.com/"+data_sku+".html"
            data_item_href = "http://item.m.jd.com/product/"+data_sku+".html"
            print(www_item_href+"\n")
            item_req = urllib.request.Request(www_item_href)
            item_response = urllib.request.urlopen(item_req, timeout=10)
            item_html = item_response.read().decode('gbk', 'ignore')
            item_soup = BeautifulSoup(item_html, 'html.parser')
            item_ul = item_soup.find_all('ul', class_='p-parameter-list')
            print(item_ul)
            # 请求评论的数据
            comment_item_href = "https://club.jd.com/comment/productPageComments.action" \
                                "?callback=fetchJSON_comment98vv756" \
                                "&productId="+data_sku+"&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0"
            comment_item_req = urllib.request.Request(comment_item_href)
            comment_item_response = urllib.request.urlopen(comment_item_req, timeout=10)
            comment_item_html = comment_item_response.read().decode('gbk', 'ignore')

            # 对返回值进行
            print(item_html)

except socket.timeout:
    print("pass")

def sleep_time(hour, minute, sec):
    return hour * 3600 + minute * 60 + sec



